package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 添加菜品和口味
     *
     * @param dishDTO
     */
    @Transactional// 开启事务
    public void saveDishWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.saveDish(dish);

        Long dishid = dish.getId();

        //获取insert语句主键值

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishid);
            }//设置菜品id
            dishMapper.saveFlavor(flavors);
        }

    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> dish = dishMapper.pageQuery(dishPageQueryDTO);

        Long total = dish.getTotal();
        List<DishVO> result = dish.getResult();
        PageResult pageResult = new PageResult(total, result);
        return pageResult;
    }

    /**
     * 批量删除菜品信息
     * @param ids
     */
    public void deleteByIds(List<Long> ids) {
        dishMapper.deleteByIds(ids);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    public DishVO getById(Long id) {

        //查询菜品
        DishDTO dishDTO = dishMapper.getById(id);
        //查询分类
        Category category =  categoryMapper.getById(dishDTO.getCategoryId());
        //查询口味
        List<DishFlavor> flavors = dishMapper.getFlavors(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dishDTO,dishVO);
        dishVO.setFlavors(flavors);
        dishVO.setCategoryId(category.getId());
        dishVO.setCategoryName(category.getName());
        return dishVO;
    }


    public void update(DishDTO dishDTO) {
        dishMapper.update(dishDTO);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        List<Dish> list = dishMapper.getByCategoryId(categoryId);
        return list;
    }
}
